package com.lmy.androidcustomview.view

import android.graphics.*
import android.graphics.drawable.Drawable
import androidx.core.graphics.toColorInt
import com.lmy.androidcustomview.dp

/**
 * @author
 * @功能:
 * @Creat 2/11/21 11:51 AM
 * @Compony 465008238@qq.com
 */


class MeshDrawable : Drawable() {
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        color = "#e75477".toColorInt()
        strokeWidth = 5.dp

    }
    private val INTERVAL = 50.dp

    //绘制
    override fun draw(canvas: Canvas) {
        var x = bounds.left.toFloat()
        var y = bounds.top.toFloat()

        while (x <= bounds.right) {
            canvas.drawLine(
                x,
                bounds.top.toFloat(),
                x,
                bounds.bottom.toFloat(),
                paint
            )
            x += INTERVAL
        }
        while (y <= bounds.bottom) {
            canvas.drawLine(
                bounds.left.toFloat(),
                y,
                bounds.right.toFloat(),
                y,
                paint
            )
            y += INTERVAL
        }
    }

    //设置透明度
    override fun setAlpha(alpha: Int) {
        paint.alpha = alpha
    }

    //获取透明度
    override fun getAlpha(): Int {
        return paint.alpha
    }

    //不透明度
    override fun getOpacity(): Int {
        return when (paint.alpha) {
            0 -> PixelFormat.TRANSPARENT
            0xff -> PixelFormat.OPAQUE
            else -> PixelFormat.TRANSLUCENT
        }
    }

    override fun setColorFilter(colorFilter: ColorFilter?) {
        paint.colorFilter = colorFilter
    }

    override fun getColorFilter(): ColorFilter? {
        return paint.colorFilter
    }

}